local mapping = {}

-- -- 配置映射(为什么要进行配置映射呢？最简单的一个例子：扫码配置码中的wifi名称为：wifissid，但是paas通过mqtt下发的wifi名称确是ssid，因为扫码是现做的，因此我就以扫码的为基准了)
-- mapping.configMapping = {netType="net_type", ssid="wifissid", pwd="wifipsk", isDHCPMod="DHCP", ip="local_ip", gateway="local_gateway", dns="local_dns", subNetmask="local_netmask"
-- , mqttAddr="tcpaddr", mqttClientId="clientid", mqttUserName="username", mqttUserPassword="userpassword", openMode="openmode", openTime="opentime", heartbeatInterval="heartbeat_itv", timeSyncInterval="timesyncinterval"
-- , deviceStatus="devicestatus", scanInterval="s_interval", recordUploadStrategy="reporttimely", checkMode="onlinecheck", faultTolerantTimeInterval="add_interval", maxFaultTolerantTime="max_ctime", analogCard="virtual_card_switch", deviceId="deviceid", devGroup="groupid"
-- , emKey="orgkey", blockNum="block_num", blockKey="block_key", blockKeyType="block_key_type"}

-- 根据上面的映射，对配置进行设置
function mapping.getConfigByMapping(configTable)
    if type(configTable) == "table" and configTable ~= nil then
        for k, v in pairs(configTable) do
            if k ~= nil and string.len(tostring(k)) > 0 and v ~= nil and string.len(tostring(v)) > 0 then
                -- local mappingKey = mapping.configMapping[k]
                -- if mappingKey ~= nil and string.len(tostring(mappingKey)) > 0 then
                -- 判断k和mappingKey是否是不能修改的配置项
                for lk, lv in pairs(mapping.cannotBeModifiedConfigMapping) do
                    if lk == lv or mapping == lv then
                        return false
                    end
                    -- end
                    -- 这里很好的防止了通过网络胡乱配置，这里判断只有mappingKey配置项在g_config中有默认配置的才能进行配置，其他的过滤掉。例如：aaaaaa:"aaaa"，aaaaaa配置项明显不存在，那么直接过滤掉
                    local configValue = g_config[k]
                    if configValue ~= nil and string.len(tostring(configValue)) > 0 then
                        g_config[k] = v
                    end
                end
            end
        end
    end
    return true
end

-- 二维码到配置文件中的映射---NET配置
mapping.netQrToConfigMapping = {
    net_type = "net_type",
    net_mode = "net_mode",
    wifissid = "wifissid",
    wifipsk = "wifipsk",
    DHCP = "DHCP",
    IP = "local_ip",
    Gateway = "local_gateway",
    DNS = "local_dns",
    Mask = "local_netmask"
}

-- paas到配置文件中的映射---NET配置
mapping.netPaasToConfigMapping = {
    netType = "net_type",
    netMode = "net_mode",
    ssid = "wifissid",
    pwd = "wifipsk",
    isDHCPMod = "DHCP",
    ip = "local_ip",
    gateway = "local_gateway",
    dns = "local_dns",
    subnetMask = "local_netmask"
}

-- paas中不能被修改的配置
mapping.cannotBeModifiedConfigMapping = {
    "sysVersion",
    "appVersion",
    "createTime",
    "uuid",
    "model",
    "disk",
    "memroy",
    "cpu",
    "status",
    "door",
    "fire"
}

-- 判断配置是否能够被修改
function mapping.isCanBeModify(configKey)
    for k, v in pairs(mapping.cannotBeModifiedConfigMapping) do
        if configKey == v then
            return false
        end
    end
    return true
end

-- 测试方法
function mapping.testGetConfigByMapping()
    local configTable = {}
    configTable.mqttAddr = "p1_addr"
    configTable.mqttClientId = "p1_clientId"
    configTable.mqttUserName = "P1_username"
    configTable.mqttUserPassword = "P1_password"
    g_config = {}
    g_config["tcpaddr"] = "g_addr"
    g_config["clientid"] = "g_clientId"
    g_config["username"] = "g_username"
    g_config["userpassword"] = "g_password"

    mapping.getConfigByMapping(configTable)

    for k, v in pairs(g_config) do
        print(k .. "---" .. v)
    end
end

-- mapping.testGetConfigByMapping()

return mapping
